package com.ys.ylideas.schedule;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.ys.ylideas.dao.CommonDao;
import com.ys.ylideas.service.CommonService;
import com.ys.ylideas.strategy.TableNameStrategy;
import com.ys.ylideas.util.CommonData;

@Component
public class ShardingReportTask {

	@Autowired
	private CommonService commonService;
	/**
	 * 每个月1号新建上个月的历史数据表
	 * 上个月报告数据保存到历史数据表并删除上个月的
	 */
	@Scheduled(cron="0 00 06 1 * ?")	
	public void ShardingAllReportTask(){
		CommonData.logger.info(new Date()+"备份数据开始");
		List<Map<String, Object>> list = new ArrayList<>();
		list.add(createMap(CommonData.BAIDU_CREATIVE_REPORT, TableNameStrategy.BYDATE));
		list.add(createMap(CommonData.BAIDU_KEYWORD_REPORT, TableNameStrategy.BYDATE));
		list.add(createMap(CommonData.BAIDU_SEARCHWORD_REPORT, TableNameStrategy.BYDATE));
		commonService.createTables(list);
		try{
			backupData(CommonData.BAIDU_CREATIVE_REPORT, TableNameStrategy.BYDATE);
		}catch (Exception e) {
			// TODO: handle exception
			CommonData.logger.error(new Date()+"备份创意报告出错");
		}
		
		try{
			backupData(CommonData.BAIDU_KEYWORD_REPORT, TableNameStrategy.BYDATE);
		}catch (Exception e) {
			// TODO: handle exception
			CommonData.logger.error(new Date()+"备份关键词报告出错");
		}
		
		try{
			backupData(CommonData.BAIDU_SEARCHWORD_REPORT, TableNameStrategy.BYDATE);
		}catch (Exception e) {
			// TODO: handle exception
			CommonData.logger.error(new Date()+"备份搜索词报告出错");
		}
		deleteHistoricData(CommonData.BAIDU_CREATIVE_REPORT, TableNameStrategy.BYDATE);
		deleteHistoricData(CommonData.BAIDU_KEYWORD_REPORT, TableNameStrategy.BYDATE);
		deleteHistoricData(CommonData.BAIDU_SEARCHWORD_REPORT, TableNameStrategy.BYDATE);
		CommonData.logger.info(new Date()+"备份数据结束");
	}
	
	
	private Map<String, Object> createMap(String tableName,TableNameStrategy strategy) {
		String tartgetTableName = getTargetTableName(tableName,strategy);
		Map<String, Object> map = new HashMap<>();
		map.put("targetTableName", tartgetTableName);
		map.put("sourceTableName", tableName);
		return map;
	}
	
	
	private String getTargetTableName(String tableName, TableNameStrategy tableNameStrategy){
		
		if (tableNameStrategy == TableNameStrategy.BYDATE) {
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH );
			String suffix;
			if (month==0) {
				year = year-1;
				month = 12;
			}
			if (month<10) {
				suffix = year+"0"+month;
			}else{
				suffix = year+""+month;
			}
			
			return tableName+"_"+suffix;	
		}
		
		return null;
	}
	
	private void backupData(String tableName, TableNameStrategy tableNameStrategy) {
		String tartgetTableName = getTargetTableName(tableName,tableNameStrategy);
		String condition = "select * from "+tableName+" where "+getCondition(tableName,tableNameStrategy);
		Map<String, Object> map = new HashMap<>();
		map.put("targetTableName", tartgetTableName);
		map.put("condition", condition);
		commonService.backupData(map);
	}
	
	private String getCondition(String sourceTableName,TableNameStrategy tableNameStrategy){
		if (tableNameStrategy == TableNameStrategy.BYDATE) {
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH );
			String date,suffixCondition;
			if (month==0) {
				year = year-1;
				month = 12;
			}
			if (month<10) {
				date = year+"-0"+month;
			}else{
				date = year+"-"+month;
			}
			suffixCondition = "LOCATE('"+date+"',import_date)";
			return suffixCondition;
		}
		return null;
	}
	
	private void deleteHistoricData(String tableName, TableNameStrategy tableNameStrategy){
		if (tableNameStrategy == TableNameStrategy.BYDATE) {
			Map<String, Object> map = new HashMap<>();
			map.put("tableName", tableName);
			map.put("condition", getCondition(tableName, tableNameStrategy));
			commonService.deleteHistoricData(map);
		}
	}
}
